package com.easy.auth.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easy.auth.domain.Impreport;
import com.easy.auth.domain.Report;
import com.easy.auth.domain.Reportlog;
import com.easy.auth.domain.UploadFile;
import com.easy.auth.service.ErrorLogService;
import com.easy.auth.service.ImpreportService;
import com.easy.auth.service.ReportService;
import com.easy.auth.service.ReportlogService;
import com.easy.auth.service.UploadService;
import com.easy.common.jdbc.PageInfo;
import com.easy.common.util.DateUtil;
import com.easy.common.util.FileUtil;
import com.easy.common.util.Parameter;
import com.easy.common.web.FilterUtil;
import com.easy.common.web.PagedList;
import com.easy.common.web.message.DeleteMessage;
import com.easy.common.web.message.Message;
import com.easy.common.web.message.SaveOrEditMessage;

@Controller
@RequestMapping("ReportController")
public class ReportController {

 
	@Autowired
	private ReportService reportService;

	@Autowired
	private ImpreportService impreportService;

	@Autowired
	private ReportlogService reportlogService;

	@Autowired
	private ErrorLogService errorLogService;
	
	@Autowired
	private  UploadService uploadService;
	/**
	 * 首页
	 * @param request
	 * @return
	 */
	
	@RequestMapping("gotoDPV")
	public String gotoDPV(HttpServletRequest request) {
		PagedList<Impreport> impreportPagedList=impreportService.getAllImpreport();
		request.setAttribute("impreportList", impreportPagedList.getRows());
		request.setAttribute("newReports", getNewReports());
		request.setAttribute("reportlogList", reportlogService.getTop10Reportlogs().getRows());
		
		List<UploadFile> uploadFileList = uploadService.getUploadFilesByType("首页车型图片").getRows();
		for (int i = 0; i < uploadFileList.size(); i++) {
			if(uploadFileList.get(i).getPartid()!=null&&!uploadFileList.get(i).getPartid().equals("")){
				uploadFileList.get(i).getPartpath();
				String k=DateUtil.getNewHtmlPathByPart(uploadFileList.get(i).getPartpath());
				uploadFileList.get(i).setLocalname("htmlimg/"+k);
			}
			uploadFileList.get(i).setLocalname("img/"+uploadFileList.get(i).getLocalname());
			
		}
		request.setAttribute("uploadFileList", uploadFileList);
		return "DPV/reportList";
		//return "DPV/uploadData";
	}
	
	@RequestMapping("getCarPic")
	@ResponseBody
	public List<UploadFile> getCarPic(HttpServletRequest request,String cartype) {
		List<UploadFile> uploadFileList = uploadService.getUploadFilesByType("车型报告图片",cartype).getRows();
		for (int i = 0; i < uploadFileList.size(); i++) {
			if(uploadFileList.get(i).getPartid()!=null&&!uploadFileList.get(i).getPartid().equals("")){
				uploadFileList.get(i).getPartpath();
				String k=DateUtil.getNewHtmlPathByPart(uploadFileList.get(i).getPartpath());
				uploadFileList.get(i).setLocalname("htmlimg/"+k);
			}
			uploadFileList.get(i).setLocalname("img/"+uploadFileList.get(i).getLocalname());
			
		}
		return uploadFileList;
	}
	
	@RequestMapping("loadAllNasReport")
	@ResponseBody
	public Map<String, String> getAllNasReport() {
		reportService.removeAllReports();
		errorLogService.removeAllErrorLogs();
		long startTime= System.currentTimeMillis();
		int number= reportService.readAllFloderAndReport(Parameter.NAS_REPORT_ROOT);
		int errrornumber= errorLogService.readAllFloderAndErrorLog(Parameter.NAS_ERRORLOG_ROOT);
		long endTime= System.currentTimeMillis();
		System.out.println("数量:"+number+"ge");
		System.out.println("用时:"+(endTime-startTime)+"ms");
		Reportlog  reportlog =new Reportlog();
		reportlog.setOdesc(errrornumber+"");
		reportlog.setReportnum(number+"");
		reportlog.setLast_modify_time(DateUtil.getSystemTime());;
		List<Reportlog> reportlogList = reportlogService.getLastFiveReportlog().getRows();
		if(reportlogList!=null&&reportlogList.size()>0){
			reportlog.setOname((Integer.parseInt(reportlog.getReportnum())-number)+"");
		}else{
			reportlog.setOname("0");
		}
		reportlogService.saveReportlog(reportlog);
		
		//return "数量:"+number+"个  用时:"+(endTime-startTime)+"ms";
		Map<String, String> map= new HashMap<String, String>();
		map.put("key", "closeMessage");
		map.put("value", Parameter.FILE_PATH);
		return map;
	}
	@RequestMapping("loadNewReport")
	@ResponseBody
	public String loadNewReport() {
		String DPV_PATH=DateUtil.getPath();
		 reportService.readNewFloderAndReport(Parameter.NAS_REPORT_ROOT);
		return "";
	}
	@RequestMapping("getAllNasReport2")
	@ResponseBody
	public String getAllNasReport2() {
		String DPV_PATH=DateUtil.getPath();
		reportService.readNewFloderAndReport(DPV_PATH);
	
		return "";
	}
	@RequestMapping("getvsd")
	@ResponseBody
	public Map<String,String> getvsd(String pdfpath) {
		
		
		
		
		Map<String, String> map= new HashMap<String, String>();
		String filepath =pdfpath;
		int lastone=0;
		lastone = filepath.lastIndexOf('\\');
		String pdfname=filepath.substring(lastone+1);
		String filename=pdfname.replace(".pdf","");
		
		int a=filepath.toLowerCase().indexOf(filename.toLowerCase()+"\\");
		if(a==-1){
			//cuowu
			map.put("path", "");
			map.put("error", "该报告的存放路径有问题，请联系管理员");
			return map;
		}
		String b=filepath.substring(a,filepath.lastIndexOf('\\')+1);
		String vsdpath=filepath.replace(b,"").replace(".pdf",".vsd").replace(Parameter.NAS_PDF_FOLDERENAME,Parameter.NAS_VSD_FOLDERNAME);
		//System.out.println(vsdpath);
		System.out.println(vsdpath);
		File vsdfile=new File(vsdpath);
		if(!vsdfile.exists()){
			map.put("path", "");
			map.put("error", "该报告的vsd的不存在！请联系相关人员");
			return map;
		}
		System.out.println(vsdpath);
		map.put("path", vsdpath.replace(Parameter.NAS_ROOT, ""));
		return map;
	}
	
 
	@RequestMapping("openFloder/uploadReport")
	@ResponseBody
	public Map<String, String> uploadReport(HttpServletRequest request,String secondMenuId) {
		//Runtime.getRuntime().exec("explorer C://FCC");
		Map<String, String> map= new HashMap<String, String>();
		map.put("key", "openFolder");
		map.put("value", Parameter.UPLOAD_REPORT_PATH);
		JSONArray jsonArray = net.sf.json.JSONArray.fromObject(map);
		//return jsonArray.toString();
		return map;
		//openFolder.jsp
	}
	@RequestMapping("openFloder/uploadData")
	@ResponseBody
	public Map<String, String> uploadData(HttpServletRequest request,String secondMenuId) {
		Map<String, String> map= new HashMap<String, String>();
		map.put("key", "openFolder");
		map.put("value", Parameter.UPLOAD_DATA_PATH);
		JSONArray jsonArray = net.sf.json.JSONArray.fromObject(map);
		//return jsonArray.toString();
		return map;
		//openFolder.jsp
	}
	@RequestMapping("openFloder/historyData")
	@ResponseBody
	public Map<String, String> historyData(HttpServletRequest request,String secondMenuId) {
		Map<String, String> map= new HashMap<String, String>();
		map.put("key", "openFolder");
		map.put("value", Parameter.HISTORY_DATA_PATH);
		JSONArray jsonArray = net.sf.json.JSONArray.fromObject(map);
		//return jsonArray.toString();
		return map;
		//openFolder.jsp
	}
	
	@RequestMapping("impReport")
	public String impReport(HttpServletRequest request){
		return "DPV/impReport";	
	}
	@RequestMapping("warning")
	public String warning(HttpServletRequest request){
		return "DPV/warning";	
	}

	@RequestMapping("upload")
	public String upload(HttpServletRequest request){
		return "upload/uploadData";	
	}
	@RequestMapping("uploadImg")
	public String uploadImg(HttpServletRequest request){
		return "DPV/uploadData";	
	}
	
	
	
	
	@RequestMapping("reportLogs")
	public String reportLogs(HttpServletRequest request){
		return "DPV/reportLogs";	
	}
	
	
	
	
	/**
	 * 查看pdf
	 * @param request
	 * @return
	 */
	
	@RequestMapping("pdf")
	public String pdf(HttpServletRequest request){
		String url=request.getParameter("url");
		request.getSession().setAttribute("url", url);
		return "DPV/pdf";	
	}
	@RequestMapping("vsd")
	public String vsd(HttpServletRequest request){
		String url=request.getParameter("url");
		String fileName = request.getParameter("fileName");
		request.getSession().setAttribute("url", url);
		request.getSession().setAttribute("fileName", fileName);
		return "DPV/fileDownLoad";	
	}
		
 
	
		@RequestMapping("goToReportManager")
		public String goToReportManager(String secondMenuId,HttpServletRequest request) {
			request.getSession().setAttribute("secondMenuId", secondMenuId);
			return "";
		}

		/**
		 * 
		 * @param oid
		 * @return
		 */
		@RequestMapping("getSonReportTree")
		@ResponseBody
		public String getSonReportTree(String oid) {
			//System.out.println("---------------"+oid);
			List<Report> reportList=reportService.getSonReportTree(oid);
			JSONArray jsonArray = net.sf.json.JSONArray.fromObject(reportList);
			return jsonArray.toString();
		}
		/**
		 * 查询报告树
		 * 
		 * @param param  分页及过滤参数
		 * @return 列表信息集合
		 */
		@RequestMapping("getReportTree")
		@ResponseBody
		public String getReportsTree(String oid) {
			PageInfo param=new PageInfo();
			param.setRows(9999);
			Report reportTree = (Report) FilterUtil.getVO(Report.class,
					param.getFilterRules());
			//reportTree.setIsfile("");
			// todo
			PagedList<Report> pageReport=reportService.findAllReportEqual(reportTree, param);
			JSONArray jsonArray = net.sf.json.JSONArray.fromObject(pageReport.getRows());
			return jsonArray.toString();
		}
		@RequestMapping("getReportCombotree")
		@ResponseBody
		public String getReportCombotree() {
			PageInfo param=new PageInfo();
			param.setRows(9999);
			Report reportTree = (Report) FilterUtil.getVO(Report.class,	param.getFilterRules());
			PagedList<Report> pageReport=reportService.findAllReportEqual(reportTree, param);
			List<Report> reportList=pageReport.getRows();
//			for (int i = 0; i < reportList.size(); i++) {
//				Report report = reportList.get(i);
//				if(report.getIsfile().endsWith("true")){
//					report.setOname(report.getOname()+"("+report.getFile_create_time()+")");	
//				}
//				
//			}
			JSONArray jsonArray = net.sf.json.JSONArray.fromObject(pageReport.getRows());
			return jsonArray.toString();
		}
		
		
		@RequestMapping("getAllReport")
		@ResponseBody
		public PagedList<Report> getAllReports(PageInfo param) {
			Report reportTree = (Report) FilterUtil.getVO(Report.class,
					param.getFilterRules());
			return reportService.findAllReportEqual(reportTree, param);
		}
		
		/**
		 * 不用了
		 * @return
		 */
		@RequestMapping("getNewReports")
		@ResponseBody
		public List<Report> getNewReports() {
			PageInfo param=new PageInfo();
			Report report = (Report) FilterUtil.getVO(Report.class,
					param.getFilterRules());
			report.setIsfile("true");
			param.setSort("file_create_time");
			param.setRows(8);
			return reportService.findAllReportEqual(report, param).getRows();
		}
		
		
		
		@RequestMapping("getReportForGrid")
		@ResponseBody
		public PagedList<Report> getReportForGrid(PageInfo param,Report report) {
			Report searchReport = (Report) FilterUtil.getVO(Report.class,
					param.getFilterRules());
			searchReport.setIsfile("true");
			//searchReport.setPid(report.getPid());
			searchReport.setId_path(","+report.getPid()+",");
			searchReport.setFile_create_time(report.getFile_create_time());
			param.setOrder("desc");
			param.setSort("file_create_time");
//			searchReport.setPid(report.getPid());
//			searchReport.setFile_time(report.getFile_time());
//			System.out.println("``````````````````"+report.getPid());
//			System.out.println("``````````````````"+report.getFile_create_time());
//			System.out.println("``````````````````"+param.getFilterRules());
			PagedList<Report> a = reportService.findAllReportLikeAndEqualPid(searchReport, param);
			
			for (int i = 0; i < a.getRows().size(); i++) {
				String k=DateUtil.getHtmlPathByPart(a.getRows().get(i).getNas_path());
				a.getRows().get(i).setMemo(k);
			}
			return a;
		}

		@RequestMapping("getReportsByTime")
		@ResponseBody
		public List<Report> getReportsByTime(String serchTime,String path,String fileName,String time){
			PagedList<Report> reportPageList=new PagedList<Report>();
			List<Report> reportList=new ArrayList<Report>();// 
			Report report = new Report();
			report.setLocal_path(path);
			report.setOname(fileName);
			report.setFile_create_time(time);
			reportList.add(report);
			//reportPageList.setRows(reportList);
			return reportList ;

		}
		
		
		/**
		 * 删除指定报告
		 * 
		 * @param oids   删除报告的id集合
		 * @return Message
		 */
		@RequestMapping("removeReport")
		@ResponseBody
		public Message removeReport(String ids) {
			return new DeleteMessage(reportService.removeReport(ids));
		}

		/**
		 * 新增或编辑报告
		 * 
		 * @param reportTree
		 * @return
		 */
		@RequestMapping("saveOrEditReport")
		@ResponseBody
		public Message saveOrEditReport(Report report) {
			return new SaveOrEditMessage(
					reportService.saveOrEditReport(report));
		}

		/**
		 * 编辑报告时查询选择的报告信息
		 * 
		 * @param reportTree
		 * @return
		 */
		@RequestMapping("findReport")
		@ResponseBody
		public List<Report> findReport(String pid) {
			Report report=new Report();
//			if(pid==null||pid.equals("")){
//				report.setLevel("2");
//			}else{
//				report.setPid(pid);
//			}
		
			report.setPid(pid);
			//System.out.println("````````````````"+report.getLevel());
			return reportService.findAllReportEqual(report, new PageInfo()).getRows();
			//return reportService.findReport(report);
		}

 
		 

}
